unit StockDataTableCut;

interface

uses StockDB, SysUtils, SyncObjs;

type
  TStockDataTableCut = class
  private
    CanUseCount,UseCount: Integer;
    TabIndex: Integer;
    DBOper: TStockDBOper;
  public
    constructor Create(ADBOper:TStockDBOper; ACanUseCount: Integer);
    destructor Destroy(); override;
    function getDataTableName():String;
  end;

var
  TableCutCS: TCriticalSection;

const
  DATA_TABLE_NAME = 'STOCK_DATA';

implementation

{ TStockDataTableCut }

constructor TStockDataTableCut.Create(ADBOper: TStockDBOper;
  ACanUseCount: Integer);
begin
  TabIndex := 1;
  ADBOper.CreateStockDataTable(DATA_TABLE_NAME + IntToStr(TabIndex));
  UseCount := 0;
  CanUseCount := ACanUseCount;
  DBOper := ADBOper;
  TableCutCS := TCriticalSection.Create;
end;

destructor TStockDataTableCut.Destroy;
begin
  TableCutCS.Free;
  inherited;
end;

function TStockDataTableCut.getDataTableName: String;
begin
  TableCutCS.Enter;
  try
    if UseCount >= CanUseCount  then
    begin
      TabIndex := TabIndex + 1;
      DBOper.CreateStockDataTable(DATA_TABLE_NAME + IntToStr(TabIndex));
      DBOper.EmptyStockDataTable(DATA_TABLE_NAME + IntToStr(TabIndex));
      UseCount := 0;
    end;
    UseCount := UseCount + 1;
    Result := DATA_TABLE_NAME + IntToStr(TabIndex);
  finally
    TableCutCS.Leave;
  end;

end;

end.
